package com.HPU.manager;

import com.HPU.entity.User;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Manager1 {
    private Scanner sc = new Scanner(System.in);
    private List<User> users = new ArrayList<User>();
    private final String TEL_PATH = "F:/tel.data";

    private void saveTel() {
        try {
            FileOutputStream out = new FileOutputStream(TEL_PATH);
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(users);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void readTel() {
        File f = new File(TEL_PATH);
        if (!f.exists()) {
            return;
        }
        try {
            FileInputStream in = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(in);
            this.users = (List<User>) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void add() {
        User user = new User();
        System.out.print("姓名:");
        user.setName(sc.next());
        System.out.print("电话号码:");
        user.setPhone(sc.next());
        users.add(user);
        System.out.println("新增成功");
        saveTel();
    }

    private void modify() {
        System.out.print("姓名:");
        String name = sc.next();
        System.out.print("请输入新的电话号码:");
        String phone = sc.next();
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i) != null && users.get(i).getName().equals(name)) {
                users.get(i).setPhone(phone);
                System.out.println("修改成功");
                break;
            }
        }

        saveTel();
    }

    private void show() {
        System.out.println("姓名\t电话");
        System.out.println("=================");
        for (User user : users) {
            if (user != null)
                System.out.println(user.getName() + "\t" + user.getPhone());
        }
        saveTel();
    }

    private void remove() {
        System.out.print("姓名:");
        String name = sc.next();
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i) != null && users.get(i).getName().equals(name)) {
                users.remove(i);
                break;
            }
        }
        System.out.println("删除成功!");
        saveTel();
    }

    public void start() {
        readTel();   // 在每次调用开始程序时导入data。
        System.out.println();
        while (true) {
            System.out.println("电话薄");
            System.out.println("===========================");
            System.out.println("1.新增");
            System.out.println("2.修改");
            System.out.println("3.查看");
            System.out.println("4.删除");
            System.out.println("5.退出");
            System.out.println("===========================");
            System.out.print("请输入功能编号：");

            String str = sc.next();
            if (str.equals("1")) {
                add();
                saveTel();
            } else if (str.equals("2")) {
                modify();
                saveTel();
            } else if (str.equals("3")) {
                show();
            } else if (str.equals("4")) {
                remove();
                saveTel();
            } else if (str.equals("5")) {
                System.out.println("成功退出!");
                break;
            } else {
                System.out.println("请重新输入");
                break;
            }
        }
    }
}

